
//
//  StudioAuthorizationTool.m
//  FHWealth
//
//  Created by fh on 2018/10/29.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "StudioAuthorizationTool.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>

@interface StudioAuthorizationTool ()

@end

@implementation StudioAuthorizationTool

/**
 判断相机是否可以用

 @return 相机是否可以用
 */
+ (BOOL)isAvailableWithCamera {
    return [self isAvailableWithDeviveMediaType:AVMediaTypeVideo];
}

/**
 异步判断相机权限：是否可用

 @param completeBlock 结果回调
 */
+ (void)asyncJudgeAvailableWithCamera:(void (^)(BOOL, BOOL, NSString *))completeBlock {
    // 在iOS7 时，只有部分地区要求授权才能打开相机
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
        // iOS 8 前，不需要授权
    }else {
        // iOS 8 后，全部都要授权
        // see to http://stackoverflow.com/a/24684021/2611971
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        switch (status) {
            case AVAuthorizationStatusNotDetermined:{
                // 许可对话没有出现，发起授权许可
                [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (granted) {
                            //第一次用户接受
                            if (completeBlock) {
                                completeBlock(YES,YES,@"用户同意了该App访问相机设备");
                            }
                        }else{
                            //用户拒绝
                            if (completeBlock) {
                                completeBlock(NO,YES,@"用户拒绝了该App访问相机设备");
                            }
                        }
                    });
                }];
                break;
            }
            case AVAuthorizationStatusAuthorized:{
                if (completeBlock) {
                    completeBlock(YES,NO,@"已经开启相机访问授权");
                }
                break;
            }
            case AVAuthorizationStatusRestricted:
                if (completeBlock) {
                    completeBlock(NO,NO,@"此应用程序没有被授权访问的照片数据。可能是家长控制权限");
                }
                break;
            case AVAuthorizationStatusDenied:
                if (completeBlock) {
                    completeBlock(NO,NO,@"用户明确地拒绝授权，或者相机设备无法访问");
                }
                break;
            default:
                if (completeBlock) {
                    completeBlock(NO,NO,@"相机设备无法访问");
                }
                break;
                
        }
    }
}

/**
 麦克风是否可用

 @return 麦克风是否可用
 */
+ (BOOL)isAvailableWithMic {
    return [self isAvailableWithDeviveMediaType:AVMediaTypeAudio];
}

/**
 异步判断麦克风权限：是否可用

 @param completeBlock 结果回调
 */
+ (void)asyncJudgeAvailableWithMic:(void (^)(BOOL, BOOL, NSString *))completeBlock {
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
        // iOS 8 前，不需要授权
    }else {
        // iOS 8 后，全部都要授权
        // see to http://stackoverflow.com/a/24684021/2611971
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
        switch (status) {
            case AVAuthorizationStatusNotDetermined:{
                // 许可对话没有出现，发起授权许可
                [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (granted) {
                            //第一次用户接受
                            if (completeBlock) {
                                completeBlock(YES,YES,@"用户同意了该App访问麦克风");
                            }
                        }else{
                            //用户拒绝
                            if (completeBlock) {
                                completeBlock(NO,YES,@"用户拒绝了该App访问麦克风");
                            }
                        }
                    });
                }];
                break;
            }
            case AVAuthorizationStatusAuthorized:{
                if (completeBlock) {
                    completeBlock(YES,NO,@"已经开启麦克风访问授权");
                }
                break;
            }
            case AVAuthorizationStatusRestricted:
                if (completeBlock) {
                    completeBlock(NO,NO,@"此应用程序没有被授权访问的麦克风数据。可能是家长控制权限");
                }
                break;
            case AVAuthorizationStatusDenied:
                if (completeBlock) {
                    completeBlock(NO,NO,@"用户明确地拒绝授权，或者麦克风无法访问");
                }
                break;
            default:
                if (completeBlock) {
                    completeBlock(NO,NO,@"麦克风无法访问");
                }
                break;
        }
    }
}

/**
 设备是否可用

 @param mediaType 媒体类型
 @return 是否可用
 */
+ (BOOL)isAvailableWithDeviveMediaType:(AVMediaType)mediaType {
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(status == ALAuthorizationStatusDenied||status == ALAuthorizationStatusRestricted){
        return NO;
    }else{
        return YES;
    }
}

/**
 相册是否可用

 @return 相册是否可用
 */
+ (BOOL)isAvailableWithPhotoLibrary {
    NSInteger status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusAuthorized) {
        return YES;
    }
    return NO;
}

/**
 异步判断相册是否可用

 @param completeBlock 结果回调
 */
+ (void)asyncJudgeAvailableWithPhotoLibrary:(void (^)(BOOL, BOOL, NSString *))completeBlock {
    NSInteger status = [PHPhotoLibrary authorizationStatus];
    switch (status) {
        case PHAuthorizationStatusNotDetermined: {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (status != PHAuthorizationStatusAuthorized) {
                            if (completeBlock) {
                                completeBlock(NO,YES,@"用户拒绝了该App访问相册");
                            }
                        }else {
                            if (completeBlock) {
                                completeBlock(YES,YES,@"用户同意了该App访问相册");
                            }
                        }
                    });
                }];
            });
        }
            break;
        case PHAuthorizationStatusRestricted:
            if (completeBlock) {
                completeBlock(NO,NO,@"已经开启相册访问授权");
            }
            break;
        case PHAuthorizationStatusDenied:
            if (completeBlock) {
                completeBlock(NO,NO,@"已经开启相册访问授权");
            }
            break;
        case PHAuthorizationStatusAuthorized:
            if (completeBlock) {
                completeBlock(YES,NO,@"已经开启相册访问授权");
            }
            break;
        default:
            if (completeBlock) {
                completeBlock(NO,NO,@"相册无法访问");
            }
            break;
    }
}

@end
